<?php include("siteTop.php"); ?>

<!--    
    Created By : Julian Ponce
    B.S. Computer Science - De La Salle Canlubang
    December 4, 2011
    Description : This will serve as the create page of Security Groups
    
-->
    
<?php 
    
?>
<!-- HEADER START -->
    <!--JAVASCRIPT SOURCES-->
    <script src="js/jquery-1.6.2.min.js"></script>

    <!--ACTUAL JS SCRIPTS AND JQUERIES-->
    <script type="text/javascript">
        jQuery(document).ready(function(){
        
            // [START]INITIALIZATION
            permissionCount = 0;
            permRowCount = 0;
            addPermissionRow();
            jQuery("#secGroupId").focus();
            // [END] INITIALIZATION
            
            
            // ADD PERMISSION BUTTON
            jQuery('#addPermBtn').live("click",function() {
                addPermissionRow();
            });
            
            // X IMAGE TO REMOVE PERMISSION
            jQuery('.removePermission').live("click",function() {
                if (permissionCount!= 1) {
                    buttonId = this.id;
                    subStr = buttonId.split('_');
                    jQuery("#permRow_" + subStr[1]).remove();
                    permissionCount--;
                }
            });
            
            // Start - Go back to admSecGroupSetup.php
            jQuery('#cancelButton').live("click",function() {
                window.location = 'admSecGroupSetup.php';
            }); // End - Go back to admSecGroupSetup.php 
            
            // [START] CREATE BUTTON CLICKED
            jQuery("#createButton").live("click",function() {
                
                // [START] VERIFY ALL REQUIRED TEXT-FIELDS
                requiredComplete = 1;
                jQuery(".required").each(function(index) {
                    if (this.value == "") {
                        requiredComplete = 0;
                    }
                });
                
                // [END] VERIFY ALL REQUIRED TEXT-FIELDS
                
                if (!requiredComplete) {
                    return false;
                }
                
                // THIS PART CHECKS DUPLICATES IN PERMISSIONS.
                else if (requiredComplete) {
                    // THIS WILL HOLD ALL PERMISSION ID's IN A STRING, DELIMITER = ','
                    allPermString = "";
                    
                    // [START] OUTER ITERATION FOR PERMISSIONS
                    noDuplicateFlag = 1;
                    jQuery(".permissionDrop").each(function(index) {
                        sameCount = 0;
                        holdSecGrp = this.value;
                        allPermString = allPermString + holdSecGrp + ",";

                        // [START] INNER ITERATION OF ALL PERMISSION DROP CLASS
                        jQuery(".permissionDrop").each(function(index) {
                            currentSecGrp = this.value;
                            if (holdSecGrp == currentSecGrp) {
                                sameCount++;
                            }
                        });
                        
                        if (sameCount > 1) {
                            noDuplicateFlag = 0;
                            alert("There must be no duplicates in Permissions.\nPlease check again.");
                            return false;
                        }
                    });
                    // [START] OUTER ITERATION FOR PERMISSIONS
                    
                    // [START] SAVE VALUES TO DATABASE
                    if (noDuplicateFlag) {
                        // CHECK OTHER SECURITY GROUPS VIA AJAX
                        jQuery.ajax({
                            type : 'POST',
                            url : 'ajaxRequests/admSaveNewSecGrp.php',
                            dataType : 'json',
                            async : false,
                            data: {
                                securityGroupId : jQuery("#secGroupId").val(),
                                securityGroupName : jQuery("#secGroupName").val(),
                                permissionCount : permissionCount,
                                permissionString : allPermString
                            },
                            success : function(data){
                                if (data) {
                                    // PLACE SUCCESS MESSAGE
                                    jQuery("#secGrpSuccessMsg").val("You have successfully created security group "+jQuery("#secGroupName").val()+"("+jQuery("#secGroupId").val()+").");
                                    
                                    // SUBMIT FORM
                                    jQuery("#secGrpSuccessForm").submit();
                                }
                            },
                            error : function() {
                                alert("fail");
                            }
                        });
                    }
                    // [END] SAVE VALUES TO DATABASE
                }
            });
            // [END] CREATE BUTTON CLICKED
            
            // [START] VERIFY SECURITY GROUP ID DUPLICATE
            jQuery("#secGroupId").live("blur",function() {
                if (this.value != "") {
                    // CHECK OTHER SECURITY GROUPS VIA AJAX
                    jQuery.ajax({
                        type : 'POST',
                        url : 'ajaxRequests/admGetSecGroupList.php',
                        dataType : 'json',
                        async : false,
                        data: {
                        },
                        success : function(data){
                            if (data) {
                                tempSecGroupId = jQuery("#secGroupId").val();
                                jQuery.each(data, function(index, data) {
                                    if (data.secGroupId == tempSecGroupId) {
                                        alert("Security Group ID Already Exist.");
                                        jQuery("#secGroupId").val("");
                                        jQuery("#secGroupId").focus();
                                    }
                                });
                            }
                        },
                        error : function() {
                            alert("fail");
                        }
                    });
                }
            });// [END] VERIFY SECURITY GROUP ID DUPLICATE
            
            
            /************ FUNCTIONS ************/
            
            // [START] addPermissionRow FUNCTION
            function addPermissionRow() {
                jQuery("#lastRowPerm").before("<tr id='permRow_"+permRowCount+"'>" + 
                    "<td><select value='asd' id='permDrop_"+permRowCount+"' class='permissionDrop'></select></td>" +
                    "<td style='text-align:center;'><img id='delImg_" + permRowCount + "' class='removePermission' src='images/ximage.png' height='20px' width='20px' style='cursor: pointer;'/></td>" +
                    "</tr>"
                );
                    
                // AJAX IN GETTING ALL PERMISSIONS
                jQuery.ajax({
                    type : 'POST',
                    url : 'ajaxRequests/admGetPermissionList.php',
                    dataType : 'json',
                    async : false,
                    data: {
                    },
                    success : function(data){
                        if (data) {
                            jQuery.each(data, function(index, data) {
                                jQuery("#permDrop_"+permRowCount).append("<option value='"+data.permissionId+"'>"+data.permissionName+"</option>");
                            });
                        }
                    },
                    error : function() {
                        alert("fail");
                    }
                });
                
                permissionCount++;
                permRowCount++;
            } 
            // [END] addPermissionRow FUNCTION
            
        }); // END - (DOCUMENT READY FUNCTION)
        
        
    </script>
    <!-- HEAD END -->


<?php include("siteMiddle.php"); ?>
    
<!-- START OF BODY -->
<div id="content">
    <h1>Create Security Group</h1>
    <form>
        <span class="screenlet" >Security Group Information</span>
        <table>
            <tr>
                <td>Security Group Id : </td>
                <td><input type="text" name="secGroupId" id="secGroupId" class="required"/></td>
            </tr>
            <tr>
                <td>Security Group Name : </td>
                <td><input type="text" name="secGroupName" id="secGroupName" class="required"/></td>
            </tr> 
        </table>
        
        <span class="screenlet" >Permissions</span>
        <table border="1">
            <tr>
                <th>Permission</th>
                <th>Action</th>
            </tr>
            <tr id="lastRowPerm">
                <td></td>
                <td style="text-align: center;"><input type="Button" name="addPermBtn" id="addPermBtn" class="siteGreenButton" value="Add"/></td>
            </tr>
        </table>
        
        <!-- CREATE BUTTON -->
        <input type="Button" name="createButton" id="createButton" class="siteGreenButton" value="Create"/>

        <!-- CANCEL BUTTON -->
        <input type="Button" name="cancelButton" id="cancelButton" class="siteRedButton" value="Cancel"/>
    </form>
    
    <!-- THIS WILL SERVE AS THE FORM FOR SUCCESS MESSAGE TO admSecGroupSetup.php -->    
    <form id="secGrpSuccessForm" action="admSecGroupSetup.php" method="POST" target="_self">
        <input type="hidden" name="secGrpSuccessMessage" id="secGrpSuccessMsg"value=""/>
    </form>

</div> 

<!-- BODY END -->
<?php 
    include ("siteBottom.php"); 
?>